<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>菜品管理</el-breadcrumb-item>
      <el-breadcrumb-item>菜品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="30">
        <el-col :span="7">
          <el-input @keyup.enter.native="serchCp(serchCPN)" placeholder="请输入菜品名" v-model="serchCPN" clearable @clear="fetch()">
            <el-button slot="append" icon="el-icon-search" @click="serchCp(serchCPN)"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="caipin" border stripe>
      <el-table-column label="#" type="index"></el-table-column>
      <el-table-column prop="cp_name" label="菜品名" width="200"></el-table-column>
      <el-table-column prop="cp_lb" label="类别" width="200" :filters="[{ text: '主菜', value: '主菜' },
      { text: '凉菜', value: '凉菜' },
      { text: '小吃', value: '小吃' },
      { text: '酒', value: '酒' },
      { text: '炒饭', value: '炒饭' },
      { text: '饮料', value: '饮料' }
      ]"
      :filter-method="filterTag"
      filter-placement="bottom-end">
      <template slot-scope="scope">
        <el-tag
          :type="scope.row.cp_lb === '主菜' ? 'primary' : 'success'"
          disable-transitions>{{scope.row.cp_lb}}</el-tag>
      </template>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="200"></el-table-column>
      <el-table-column prop="mssage" label="菜品介绍"></el-table-column>
      <el-table-column label="操作" align="right">
        <template slot-scope="scope">
          <el-tooltip content="修改信息" placement="top" effect="light" :enterable="false">
            <el-button
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row.cp_id)"
              type="primary"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除信息" placement="top" effect="light" :enterable="false">
            <el-button
              size="mini"
              icon="el-icon-delete"
              @click="handleDelete(scope.row.cp_id)"
              type="danger"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage"
        :page-sizes="[5, 10, 15, 30]"
        :page-size=queryInfo.pageSize
        layout="total, sizes, prev, pager, next, jumper"
        :total="queryInfo.total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      caipin: [],
      queryInfo: {
        currentPage: 1,
        pageSize: 5,
        total: 0
      },
      serchCPN: ''
    }
  },
  methods: {
    async fetch() {
      const { data: res } = await this.$http.get('/cp', {
        params: this.queryInfo
      })
      if (res.status === 403) {
        this.$message({
          type: 'error',
          message: '登录已失效，请重新登录！'
        })
        this.$router.push('/login')
        return false
      }
      if (res.meta.status !== 200) {
        return this.$message.error('获取菜品列表失败！')
      }
      this.caipin = res.caipinxx
      this.queryInfo = res.pagination
    },
    async serchCp(val) {
      const { data: res } = await this.$http.get(`cp?cpname=${val}`)
      if (res.status === 403) {
        this.$message({
          type: 'error',
          message: '登录已失效，请重新登录！'
        })
        this.$router.push('/login')
        return false
      }
      this.caipin = res.caipinxx
      this.queryInfo = res.pagination
    },
    filterTag(value, row) {
      return row.cp_lb === value
    },
    // 监听pagesize改变的事件
    handleSizeChange(val) {
      this.queryInfo.pageSize = val
      this.fetch()
    },
    // 页码值改变的时间
    handleCurrentChange(val) {
      this.queryInfo.currentPage = val
      this.fetch()
    },
    handleEdit(id) {
      this.$router.push(`${id}/editcp`)
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除菜品信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http.delete(`cp${id}`).then(res => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetch()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  created() {
    this.fetch()
  }
}
</script>
